package com.xuning.emperor.controller;

import com.xuning.emperor.base.Result;
import com.xuning.emperor.business.IUserBusiness;
import com.xuning.emperor.domain.dto.UserDTO;
import com.xuning.emperor.domain.vo.UserVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author wangqx
 * @description 用户的控制器.
 * @date 2022/6/16
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/api/user")
public class UserController {

    /**
     * 业务层 .
     */
    @Autowired
    private IUserBusiness userBusiness;


    /**
     * 新增.
     *
     * @param req
     * @return
     */
    public Result add(@RequestBody final UserDTO req) {
        return Result.ok(userBusiness.save(req));
    }

    /**
     * 修改.
     *
     * @param req
     * @return
     */
    @PutMapping("")
    public Result<Boolean> edit(@RequestBody @Valid final UserDTO req) {
        return Result.ok(userBusiness.save(req));
    }

    /**
     * 列表.
     *
     * @param req
     * @return
     */
    @GetMapping("/")
    public Result<List<UserVO>> list(@Valid final UserDTO req) {
        return Result.ok(userBusiness.list(req));
    }

    /**
     * 详情.
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}/")
    public Result<UserVO> info(@PathVariable final Long id) {
        return Result.ok(userBusiness.get(id));
    }

    /**
     * 分页.
     *
     * @param req 分页对象.
     * @return
     */
    @GetMapping("/page")
    public Result<IPage<UserVO>> page(IPage<UserVO> page, @Valid final UserDTO req) {
        return Result.ok(userBusiness.page(page, req));
    }


    /**
     * 删除 .
     *
     * @param id 主键
     * @return
     */
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@PathVariable final Long id) {
        return Result.ok(userBusiness.delete(id));
    }


}
